﻿package plugin.union.game
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.display.*;
    import com.sh.game.util.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.union.*;
    import uiresource.union.*;

    public class UnionHongBaoGame extends HongbaoUI implements IPage
    {
        private var leftNum:int;
        private var endTime:int;
        private var money:int;
        private var RedPacketRecord:Array;
        private var _totalPage:int = 0;
        private var _curPage:int = 0;
        private var _mc:XMLMovieClip;
        private var _mc2:XMLMovieClip;
        private var _mc3:XMLMovieClip;
        private var _timeID:uint = 0;
        private var _timeID2:uint = 0;
        private var _timeID3:uint = 0;

        public function UnionHongBaoGame()
        {
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            this.initEvents();
            yuanbaotips.visible = false;
            return;
        }// end function

        private function initEvents() : void
        {
            zuoBtn.clickHandler = new Handler(this.zuoBtnHandler);
            youBtn.clickHandler = new Handler(this.youBtnHandler);
            shuomingLink.toolTip = "1.所有会员可发放红包，所有会员可参与抢红包\n" + "2.所抢红包金额随机发放\n" + "3.红包" + MColor.getHMTL("当天有效", 62) + "\n" + "4.红包有效期过后，剩余红包里的元宝将通过邮件返还给发红包的玩家\n" + "5.抢红包期间，玩家不可发红包";
            fafangBtn.clickHandler = new Handler(this.fafangBtnHandler);
            qiangBtn.clickHandler = new Handler(this.qiangBtnHandler);
            zongyuanbao.text = "1000";
            zongyuanbao.restrict = "0-9";
            zongyuanbao1.text = "10";
            zongyuanbao1.restrict = "0-9";
            return;
        }// end function

        private function qiangBtnHandler() : void
        {
            if (this.money > 0)
            {
                ModelProxy.change("movetips", "error", "您已经领取过红包");
                return;
            }
            if (this.leftNum == 0)
            {
                ModelProxy.change("movetips", "error", "当前没有红包可领");
                return;
            }
            if (this._mc)
            {
                if (this._mc.parent)
                {
                    this._mc.parent.removeChild(this._mc);
                }
                this._mc.stop();
            }
            this._mc2 = XMLMcManager.instance.getPluginMc("union", "redpacketopen");
            if (this._mc2)
            {
                this._mc2.mouseChildren = false;
                this._mc2.mouseEnabled = false;
                this._mc2.x = 187;
                this._mc2.y = -48;
                this._mc2.interval = 1000 / 12;
                this.addChild(this._mc2);
                this._mc2.playFromTo(0, (this._mc2.totalFrame - 1), new Handler(this.playComPlete));
            }
            UnionSender._69sendRedPacket();
            return;
        }// end function

        private function playComPlete() : void
        {
            if (this._mc2)
            {
                if (this._mc2.parent)
                {
                    this._mc2.parent.removeChild(this._mc2);
                }
                this._mc2.stop();
            }
            return;
        }// end function

        private function fafangBtnHandler() : void
        {
            UnionSender._68sendRedPacket(int(zongyuanbao.text), int(zongyuanbao1.text));
            return;
        }// end function

        private function youBtnHandler() : void
        {
            if (this._curPage < this._totalPage)
            {
                var _loc_1:String = this;
                var _loc_2:* = this._curPage + 1;
                _loc_1._curPage = _loc_2;
            }
            this.setlistPage();
            return;
        }// end function

        private function zuoBtnHandler() : void
        {
            if (this._curPage > 0)
            {
                var _loc_1:String = this;
                var _loc_2:* = this._curPage - 1;
                _loc_1._curPage = _loc_2;
            }
            this.setlistPage();
            return;
        }// end function

        private function setlistPage() : void
        {
            list.page = this._curPage;
            fenye.text = (this._curPage + 1) + "/" + (this._totalPage + 1);
            return;
        }// end function

        public function refresh() : void
        {
            var _loc_1:* = ModelProxy.getValue("data", "unionredpacket");
            this.RedPacketRecord = [];
            this.leftNum = 0;
            this.endTime = 0;
            this.money = 0;
            if (_loc_1)
            {
                this.leftNum = _loc_1.leftNum;
                this.endTime = _loc_1.endTime;
                this.money = _loc_1.money;
                this.RedPacketRecord = _loc_1.redPacketRecords;
            }
            if (this.RedPacketRecord)
            {
                this._totalPage = (this.RedPacketRecord.length - 1) / 9;
            }
            else
            {
                this.RedPacketRecord = [];
                this._totalPage = 0;
            }
            if (this._totalPage == 0)
            {
                zuoBtn.disabled = true;
                youBtn.disabled = true;
            }
            else
            {
                zuoBtn.disabled = false;
                youBtn.disabled = false;
            }
            this._curPage = 0;
            list.repeatY = 9;
            var _loc_2:Array = [];
            var _loc_3:int = 0;
            while (_loc_3 < this.RedPacketRecord.length)
            {
                
                _loc_2.push(this.RedPacketRecord[(this.RedPacketRecord.length - 1) - _loc_3]);
                _loc_3++;
            }
            this.RedPacketRecord = _loc_2;
            list.array = this.RedPacketRecord;
            list.renderHandler = new Handler(this.ItemRender);
            if (this._timeID > 0)
            {
                clearInterval(this._timeID);
                this._timeID = 0;
            }
            if (this.leftNum == 0)
            {
                over2.visible = false;
                over1.visible = true;
                n1.url = "png.common.cs2_up0";
                n2.url = "png.common.cs2_up0";
                n2.url = "png.common.cs2_up0";
                sendbox.visible = true;
            }
            else
            {
                over1.visible = false;
                over2.visible = true;
                n1.url = "png.common.cs2_up" + this.leftNum % 10;
                n2.url = "png.common.cs2_up" + int(this.leftNum / 10) % 10;
                n3.url = "png.common.cs2_up" + int(this.leftNum / 100);
                sendbox.visible = false;
                this.lasttimeClick();
                this._timeID = setInterval(this.lasttimeClick, 1000);
            }
            if (this.leftNum > 0 && this.money == 0)
            {
                this._mc = XMLMcManager.instance.getPluginMc("union", "redpacket");
                if (this._mc)
                {
                    this._mc.mouseChildren = false;
                    this._mc.mouseEnabled = false;
                    this._mc.x = 187;
                    this._mc.y = -48;
                    this._mc.interval = 1000 / 12;
                    this.addChild(this._mc);
                }
                this._mc3 = XMLMcManager.instance.getPluginMc("union", "redpacketbtn");
                if (this._mc3)
                {
                    this._mc3.mouseChildren = false;
                    this._mc3.mouseEnabled = false;
                    this._mc3.x = 442 - 32;
                    this._mc3.y = 425 - 74;
                    this._mc3.interval = 1000 / 12;
                    this.addChild(this._mc3);
                }
            }
            else
            {
                if (this._mc)
                {
                    if (this._mc.parent)
                    {
                        this._mc.parent.removeChild(this._mc);
                    }
                    this._mc.stop();
                }
                if (this._mc3)
                {
                    if (this._mc3.parent)
                    {
                        this._mc3.parent.removeChild(this._mc3);
                    }
                    this._mc3.stop();
                }
            }
            if (this.money > 0 && this.leftNum > 0)
            {
                qiangBtn.disabled = true;
                yiqiangduo.visible = true;
            }
            else
            {
                qiangBtn.disabled = false;
                yiqiangduo.visible = false;
            }
            this.setlistPage();
            return;
        }// end function

        private function lasttimeClick() : void
        {
            var _loc_1:* = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
            var _loc_2:* = this.endTime - _loc_1;
            lefttime.text = "红包有效倒计时：" + TimeDataFormatUtil.getLeftTime2(_loc_2);
            return;
        }// end function

        private function ItemRender(param1:Component, param2:int) : void
        {
            if (param2 < this.RedPacketRecord.length)
            {
                if (param1.dataSource.type == 0)
                {
                    (param1.getChildByName("playname") as Label).text = "<font color = \'#766b62\'>【</font><font color = \'#b9774b\'>" + param1.dataSource.name + "</font><font color = \'#766b62\'>】</font>" + "<font color = \'#766b62\'>获得</font><font color = \'#ffd700\'>" + param1.dataSource.money + "</font><font color = \'#766b62\'>元宝</font>";
                }
                else
                {
                    (param1.getChildByName("playname") as Label).text = "<font color = \'#766b62\'>【</font><font color = \'#b9774b\'>" + param1.dataSource.name + "</font><font color = \'#766b62\'>】</font>" + "<font color = \'#e19f21\'>发了</font><font color = \'#ffd700\'>" + param1.dataSource.money + "</font><font color = \'#e19f21\'>元宝</font>";
                }
            }
            return;
        }// end function

        private function getYB(param1:int) : void
        {
            num1.visible = false;
            num2.visible = false;
            num3.visible = false;
            num4.visible = false;
            if (param1 >= 10000)
            {
                num1.visible = true;
                num1.url = "png.common.cs2_w";
                num2.url = "png.common.cs2_" + int(param1 / 10 / 1000) % 10;
                num3.url = "png.common.cs2_" + int(param1 / 100 / 1000) % 10;
                num4.url = "png.common.cs2_" + int(param1 / 1000 / 1000);
                if (num4.url == "png.common.cs2_0")
                {
                    num4.visible = false;
                }
                else
                {
                    num4.visible = true;
                }
                if (num4.url == "png.common.cs2_0" && num3.url == "png.common.cs2_0")
                {
                    num3.visible = false;
                }
                else
                {
                    num3.visible = true;
                }
                if (num4.url == "png.common.cs2_0" && num3.url == "png.common.cs2_0" && num2.url == "png.common.cs2_0")
                {
                    num2.visible = false;
                }
                else
                {
                    num2.visible = true;
                }
            }
            else
            {
                num1.url = "png.common.cs2_" + param1 % 10;
                num2.url = "png.common.cs2_" + int(param1 / 10) % 10;
                num3.url = "png.common.cs2_" + int(param1 / 100) % 10;
                num4.url = "png.common.cs2_" + int(param1 / 1000);
                if (num4.url == "png.common.cs2_0")
                {
                    num4.visible = false;
                }
                else
                {
                    num4.visible = true;
                }
                if (num4.url == "png.common.cs2_0" && num3.url == "png.common.cs2_0")
                {
                    num3.visible = false;
                }
                else
                {
                    num3.visible = true;
                }
                if (num4.url == "png.common.cs2_0" && num3.url == "png.common.cs2_0" && num2.url == "png.common.cs2_0")
                {
                    num2.visible = false;
                }
                else
                {
                    num2.visible = true;
                }
            }
            if (this._timeID2 > 0)
            {
                yuanbaotips.visible = false;
                clearTimeout(this._timeID2);
                this._timeID2 = 0;
            }
            if (this._timeID3 > 0)
            {
                clearTimeout(this._timeID3);
                this._timeID3 = 0;
            }
            yuanbaotips.parent.setChildIndex(yuanbaotips, (yuanbaotips.parent.numChildren - 1));
            this._timeID3 = setTimeout(this.showyaobaoTips, 500);
            return;
        }// end function

        private function showyaobaoTips() : void
        {
            yuanbaotips.visible = true;
            this._timeID2 = setTimeout(function () : void
            {
                yuanbaotips.visible = false;
                return;
            }// end function
            , 2000);
            return;
        }// end function

        public function update(param1:int, param2:Object) : void
        {
            switch(param1)
            {
                case CMDUnion._67ResRedPacketInfo:
                {
                    this.refresh();
                    break;
                }
                case CMDUnion._70ResGetRedPacket:
                {
                    this.getYB(int(param2));
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            return;
        }// end function

    }
}
